//
//  AXFMyOrderDetailViewController.m
//  AIXF
//
//  Created by zhanghui on 2017/7/14.
//  Copyright © 2017年 Macx. All rights reserved.
//

#import "AXFMyOrderDetailViewController.h"
#import "AXFMyOrderViewModel.h"
#import "AXFOrderStatusModel.h"
#import "AXFMyOrderDetailCell.h"
#import "AXFMyOrderDetailsMoreCell.h"

@interface AXFMyOrderDetailViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (strong,nonatomic) UITableView *orderStatusTableView;

@property (nonatomic,strong)UITableView *orderDetailTableView;

@end

@implementation AXFMyOrderDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //初始化
    [self setupUI];
    
    //创建订单详情tableView
    [self setupDetailTableView];
    
    //创建订单statusTableView
    [self setupStatusTableView];
}

#pragma mark - 初始化UI
- (void)setupUI{
    
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    //设置titleView
    NSArray *itemArr = @[@"订单状态",@"订单详情"];
    
    UISegmentedControl *segmengtCtr = [[UISegmentedControl alloc]initWithItems:itemArr];
    
    //设置边框颜色
    segmengtCtr.layer.borderColor = [UIColor orangeColor].CGColor;
    
    //设置选中颜色
    segmengtCtr.tintColor = [UIColor orangeColor];
    
    //设置点击时间
    [segmengtCtr addTarget:self action:@selector(changeOrderView:) forControlEvents:UIControlEventValueChanged];
    
    //默认选中第0个
    segmengtCtr.selectedSegmentIndex = 0;
    
    self.navigationItem.titleView = segmengtCtr;
    
    //右边的投诉
    UIBarButtonItem *rightItem = [[UIBarButtonItem alloc]initWithTitle:@"投诉" style:0 target:nil action:nil];
    
    self.navigationItem.rightBarButtonItem = rightItem;
}

- (void)changeOrderView:(UISegmentedControl *)segment{
    
    //根据选中的值，添加tbaleView到self.view上
    if (segment.selectedSegmentIndex == 0) {
    
        [self.view bringSubviewToFront:self.orderStatusTableView];
        
    }else{
        
        [self.view bringSubviewToFront:self.orderDetailTableView];
    
    }
    
}

#pragma mark - 初始化statusTableView
- (void)setupStatusTableView{
    
    CGRect tableViewRect = CGRectMake(0, MyOrderStatusTableViewY, ScreenWidth, MyOrderStatusTableViewHeight);
    
    
    UITableView *table = [[UITableView alloc]initWithFrame:tableViewRect style:UITableViewStylePlain];
    
    //设置代理
    self.orderStatusTableView = table;
    
    table.dataSource = self;
    table.delegate = self;
    
    //注册cell
    [table registerNib:[UINib nibWithNibName:@"AXFMyOrderDetailCell" bundle:nil] forCellReuseIdentifier:MyOrderStatusTableCellID];
    
    //设置预估行高
    table.rowHeight = 90;
    
    table.separatorInset = UIEdgeInsetsMake(0, 75, 0, 0);
    
    
    [self.view addSubview:self.orderStatusTableView];
}

#pragma mark - 初始化订单详情tableView
- (void)setupDetailTableView{
    
    
    CGRect tableViewRect = CGRectMake(0, MyOrderStatusTableViewY, ScreenWidth, ScreenHeight);

    
    UITableView *detail = [[UITableView alloc]initWithFrame:tableViewRect style:UITableViewStyleGrouped];
    
    //设置代理
    self.orderDetailTableView = detail;
    
    detail.dataSource = self;
    detail.delegate = self;
    
    //注册cell
    [self.orderDetailTableView registerNib:[UINib nibWithNibName:@"AXFMyOrderDetailsMoreCell" bundle:nil] forCellReuseIdentifier:MyOrderDetailMoreCellId];
    
    //设置预估行高
    detail.rowHeight = 260;
    
    [self.view addSubview:self.orderDetailTableView];
    
    
}

#pragma mark - 多少组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    
    return tableView == self.orderStatusTableView ? 1: 4;
}



- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
 
    if (tableView == self.orderStatusTableView) {

        return _MyOrderVieModel.orderViewModel[section].orderModel.status_timeline.count;
    }
    
    return  1;
    
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if (tableView == self.orderStatusTableView) {
        
        AXFMyOrderDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:MyOrderStatusTableCellID forIndexPath:indexPath];
        
        //取出模型
        AXFOrderStatusModel *statusModel = _MyOrderVieModel.orderViewModel[indexPath.row].orderModel.status_timeline[indexPath.row];
        
        //设置数据
        cell.statusModel = statusModel;
        
        return cell;
        
    }
    
    //订单详情cell
    if (indexPath.section == 0) {
        
        AXFMyOrderDetailsMoreCell *cell = [tableView dequeueReusableCellWithIdentifier:MyOrderDetailMoreCellId forIndexPath:indexPath];
        
        cell.orderModel = _MyOrderVieModel.orderViewModel[indexPath.section].orderModel;
        
        return cell;
        
      
        
    }
    
    //防止ben
    UITableViewCell* undefineCell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:WrongCellID];
    
    [undefineCell setHidden:YES];
    
    return undefineCell;
    
    
}



@end
